package com.ctgu.tengyun.bean.convert;

import com.ctgu.tengyun.bean.request.SaveProductGuideRequest;
import com.ctgu.tengyun.bean.response.*;
import com.ctgu.tengyun.domain.*;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by liaoweizhi on 2019/7/21.
 */
public class ProductGuideConvert {

    public static ProductGuideInfo fromSaveProductGuideRequest(SaveProductGuideRequest request) {
        Validator.assertHasFields(!StringUtils.isEmpty(request.getProductNo()), "productNo is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(request.getGuideName()), "productGuideName is required.");
        Validator.assertHasFields(ProductGuideType.isValid(request.getGuideType()), "guideType is required.");
        Validator.assertHasFields(null != request.getGuideFile(), "guideFile is required.");
        FileConvert.splitFileBeanUrlPath(request.getGuideFile());

        ProductGuideInfo guideInfo = new ProductGuideInfo();
        guideInfo.setId(request.getId());
        guideInfo.setName(request.getGuideName());
        guideInfo.setGuideType(request.getGuideType());
        guideInfo.setGuideFile(request.getGuideFile());
        guideInfo.setRemark(request.getRemark());

        return guideInfo;
    }

    public static ProductGuideListResponse toProductGuideListResponse(List<ProductGuideInfo> guideInfoList,
                                                                      Map<Integer, ProductInfo> idProductInfoMap) {
        ProductGuideListResponse response = new ProductGuideListResponse();

        List<GuideDetail> guideList = new ArrayList<>(guideInfoList.size());
        guideInfoList.forEach(item -> {
            GuideDetail guideDetail = new GuideDetail();
            BeanUtils.copyProperties(item, guideDetail);
            guideDetail.setProductNo(idProductInfoMap.get(item.getPid()).getProductNo());
            guideDetail.setProductName(idProductInfoMap.get(item.getPid()).getName());
            guideDetail.setGuideName(item.getName());
            guideDetail.setGuideFile(FileConvert.fillFileBeanUrlPath(item.getGuideFile()));
            guideList.add(guideDetail);
        });

        response.setGuideList(guideList);
        return response;
    }

    public static ProductLogListResponse toProductLogListResponse(List<ProductGuideLog> guideLogList,
                                                                  Map<Long, SysUser> idSysUserMap) {
        ProductLogListResponse response = new ProductLogListResponse();
        response.setGuideLogList(toGuideLogBean(guideLogList, idSysUserMap));

        return response;
    }

    public static ProductHistoryResponse toProductHistoryResponse(ProductInfo productInfo,
                                                                  List<ProductGuideLog> guideLogList,
                                                                  Map<Long, SysUser> idSysUserMap) {

        ProductHistoryResponse response = new ProductHistoryResponse();
        response.setProductNo(productInfo.getProductNo());
        response.setProductName(productInfo.getName());

        response.setGuideLogList(toGuideLogBean(guideLogList, idSysUserMap));

        return response;
    }

    private static List<GuideLogBean> toGuideLogBean(List<ProductGuideLog> guideLogList, Map<Long, SysUser> idSysUserMap) {
        List<GuideLogBean> guideLogBeanList = new ArrayList<>(guideLogList.size());
        guideLogList.forEach(item -> {
            GuideLogBean guideLogBean = new GuideLogBean();
            BeanUtils.copyProperties(item, guideLogBean);
            SysUser sysUser = idSysUserMap.get(item.getOperationUserId());
            guideLogBean.setOperationNickName(null == sysUser ? null : sysUser.getNickName());
            guideLogBean.setGuideFile(FileConvert.fillFileBeanUrlPath(item.getGuideFile()));

            guideLogBeanList.add(guideLogBean);
        });

        return guideLogBeanList;
    }

}
